package org.jeecg.modules.supplier.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.supplier.entity.qualification;
import org.jeecg.modules.supplier.mapper.QualificationMapper;
import org.jeecg.modules.supplier.service.IQualificationService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 供应商资质表服务实现类
 */
@Service
public class QualificationServiceImpl extends ServiceImpl<QualificationMapper, qualification> implements IQualificationService {
    @Override
    public List<qualification> getQualificationList(String supplierid) {
        //根据supplierid查询对应的资质列表
        return this.lambdaQuery()
                .eq(qualification::getSupplierId, supplierid)
                .list();
    }

    @Override
    public boolean updatequalification(List<qualification> qualifications, String supplierId) {
        // 先删除该供应商的所有现有资质
        this.lambdaUpdate()
                .eq(qualification::getSupplierId, supplierId)
                .remove();

        // 设置所有新资质的供应商ID和更新时间
        Date now = new Date();
        for (qualification qual : qualifications) {
            qual.setSupplierId(supplierId);
            qual.setUpdateTime(now);
            // 如果是新增的资质，设置创建时间
            if (qual.getCreateTime() == null) {
                qual.setCreateTime(now);
            }
        }

        // 批量保存新的资质列表
        return this.saveBatch(qualifications);
    }
}
